/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.dao;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.cmcc.zysoft.infocollection.model.RoleMenu;
import com.starit.common.dao.hibernate.HibernateBaseDaoImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: RoleMenuPcDao.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-6 上午9:34:50
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Repository
public class RoleMenuPcDao extends HibernateBaseDaoImpl<RoleMenu, String> {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 通过角色ID获取角色对应的菜单.
	 * @param roleId 角色ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> getMenuByRoleId(String roleId) {
		String sql = 
				" SELECT a.* " +
				" FROM tb_c_role_menu a, tb_c_menu b " +
				" WHERE a.menu_id = b.menu_id AND b.is_leaf = 'Y' AND b.status = '1' AND a.role_id = ? ";
		return this.jdbcTemplate.queryForList(sql, roleId);
	}
	
	/**
	 * 根据父菜单ID查找该角色权限下的其子菜单数量.
	 * @param roleId 角色ID
	 * @param parentId 父菜单ID
	 * @return int
	 */
	public int getLeafCount(String roleId, String parentId) {
		String sql = 
				" SELECT COUNT(*) " +
				" FROM tb_c_role_menu a, tb_c_menu b " +
				" WHERE a.menu_id = b.menu_id AND a.role_id = ? AND b.parent_id = ?";
		return this.jdbcTemplate.queryForInt(sql, roleId, parentId);
	}
}